package com.lineage.server.Instance;

import com.lineage.DatabaseFactory;
import com.lineage.server.datatables.map.MapLimitSettingTable;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class MapLimitSettingInstance {
  private static final Log log = LogFactory.getLog(MapLimitSettingInstance.class);
  
  private static final Map<Integer, MapLimitSettingTable> mapLimitList = new HashMap<>();
  
  private static MapLimitSettingInstance instance;
  
  private MapLimitSettingInstance() {
    load();
  }
  
  public void load() {
    Connection co = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
      co = DatabaseFactory.get().getConnection();
      ps = co.prepareStatement("SELECT * FROM `w_map_limit_setting`");
      rs = ps.executeQuery();
      while (rs.next()) {
        MapLimitSettingTable mapLimitSettingTable = new MapLimitSettingTable();
        mapLimitSettingTable.setMapId(Integer.valueOf(rs.getInt("map_id")));
        mapLimitSettingTable.setLimitType(Integer.valueOf(rs.getInt("limit_type")));
        mapLimitList.put(mapLimitSettingTable.getMapId(), mapLimitSettingTable);
      } 
    } catch (SQLException e) {
      log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(ps);
      SQLUtil.close(co);
    } 
    log.info("载入地图限制设定:" + mapLimitList.size());
  }
  
  public static MapLimitSettingInstance get() {
    if (instance == null)
      instance = new MapLimitSettingInstance(); 
    return instance;
  }
  
  public MapLimitSettingTable getConfigByMapId(Integer mapId) {
    return mapLimitList.get(mapId);
  }
}
